package hongcaosp.app.android.common;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseHolder<T>> {

    private List<T> data;

    public void setData(List<T> data) {
        this.data = data;
    }

    public List<T> getData() {
        return data;
    }

    public void addData(List<T> data) {
        if (this.data == null) {
            this.data = new ArrayList<>();
        }
        if (data != null && data.size() > 0) {
            this.data.addAll(data);
        }
    }

    public T getItem(int position) {
        if (data == null || position < 0 || position >= data.size()) {
            return null;
        }
        return data.get(position);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseHolder<T> holder, int position) {
        holder.setData(data.get(position));
    }

    @Override
    public int getItemCount() {
        return this.data == null ? 0 : this.data.size();
    }
}
